package com.fzy.auth.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.fzy.auth.entity.User;
import com.fzy.auth.service.UserService;
import com.fzy.core.BaseAction;
import com.fzy.core.MyValidationAware;
import com.opensymphony.webwork.util.TokenHelper;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ModelDriven;
import com.opensymphony.xwork.Preparable;
import com.opensymphony.xwork.util.LocalizedTextUtil;

public class UserSaveAction extends BaseAction implements ModelDriven,
		Preparable, MyValidationAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2513266197089818899L;
	User user;
	String roleIds;
	UserService userService;
	String errorDispatch;

	public void prepare() throws Exception {
		errorDispatch = "dispatcher:/WEB-INF/jsp/auth/user-input.jsp";
		
        Map session = ActionContext.getContext().getSession();
        synchronized (session) {
            if (!TokenHelper.validToken()) {
                addActionError(getText("error.duplicateSubmission"));
            }
        }
        if(!hasActionErrors()){
    		user = (User)validateId(request.getParameter("user.id"), User.class);
    		if(hasActionErrors()){
    			errorDispatch = "redirect:userList.do";
    		}
        }

	}

	public void validate() {
		
	}

	public String execute() throws Exception {
		userService.saveEntityId(user);

		addActionMessage(getText("success.save"));
		return "redirect:/auth/userList.do";
	}

	public Object getModel() {
		return user;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getRoleIds() {
		return roleIds;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getErrorDispatch() {
		return errorDispatch;
	}
	
}
